package com.slipper.common.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

public class JwtUtil {
    private static final Logger logger = LoggerFactory.getLogger(JwtUtil.class);
    
    static final String SECRET = "BoddonAPPSecret";
    static final String HEADER_NAME = "Authorization";

    public static String headerName() {
        return HEADER_NAME;
    }
    
    public static String generateToken(int minute) {
        return generateToken(minute, null);
    }
    
    public static String generateToken(int minute, Map<String, Object> dataMap) {
        return generateToken(SECRET, minute, dataMap);
    }
    
    public static String generateToken(String secret, int minute, Map<String, Object> dataMap) {
        if (dataMap == null) {
            dataMap = new HashMap<>();
        }
        Date generateTime = new Date();
        dataMap.put("generateTime", DateUtil.formatTime(generateTime));
        return Jwts.builder()
                .setClaims(dataMap)
                .setExpiration(new Date(generateTime.getTime() + (long) minute * 60 * 1000))
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }
    
    public static Map<String, Object> parserToken(String token) {
        return parserToken(SECRET, token);
    }
    
    public static Map<String, Object> parserToken(String secret, String token) {
        if (token == null) {
            logger.error("token不能为空！");
            return null;
        }
        
        try {
            return Jwts
                    .parser()
                    .setSigningKey(secret)
                    .parseClaimsJws(token)
                    .getBody();
        } catch (ExpiredJwtException e) {
            logger.error("token解释出错: {}, token: {}", e.getMessage(), token);
            return null;
        }
    }
    
    public static boolean validateToken(String token) {
        return validateToken(SECRET, token);
    }
    
    public static boolean validateToken(String secret, String token) {
        if (token == null) {
            logger.error("token不能为空！");
            return false;
        }
        
        try {
            Map<String, Object> body = Jwts
                    .parser()
                    .setSigningKey(secret)
                    .parseClaimsJws(token)
                    .getBody();
            return !body.isEmpty();
        } catch (Exception e) {
            logger.error("token验证出错: " + token, e);
            return false;
        }
    }
    
    public static void main(String[] args) {
        for (int i=0;i<1;i++) {
            String token = generateToken(1);
            System.out.println(token);
            System.out.println(validateToken(token, "cc"));
        }
        System.out.println(parserToken("eyJhbGciOiJIUzUxMiJ9.eyJnZW5lcmF0ZVRpbWUiOiIyMDIwLTEyLTA1IDExOjQ4OjI1IiwiZXhwIjoxNjA3MTQwMTY1fQ.y9xkz9aZquAOiB3T4P3HIwFH_8MHwQ95wIygS087cQbnMTILzXS5I471KJebYT935aKGgeUDCRg9ufMAl7_Aaw"));
    }
}
